//
//  QuickPublishView.m
//  yigou
//
//  Created by wangdianlei on 2017/1/12.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "QuickPublishView.h"
#import "AppDelegate.h"
#import "CYLTabBarController.h"
#import "PublishTopicViewController.h"
#import "PublishGoodViewController.h"
#import "PublishArticleViewController.h"
#import "CreateTribeViewController.h"

static const NSInteger kViewTag = 3000;

@implementation QuickPublishView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _bgview = [[UIView alloc] initWithFrame:frame];
        _bgview.backgroundColor = [UIColor colorWithHexString:@"222222"];
        _bgview.alpha = 0.8;
        [self addSubview:_bgview];
        _bgview.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBg:)];
        tap.numberOfTapsRequired = 1;
        [_bgview addGestureRecognizer:tap];
        
        _cancelbtn= [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelbtn.frame = CGRectMake(ScreenWidth / 2 - 63 / 2, ScreenHeight - 63 - 15, 63, 63);
        [_cancelbtn setBackgroundImage:[UIImage imageNamed:@"pubcancel"] forState:UIControlStateNormal];
        [_cancelbtn addTarget:self action:@selector(clickCancel:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelbtn];
        
        CGFloat interx = 15;
        CGFloat width = (ScreenWidth - 15 * 6) / 3;
//        _view1 = [[UIView alloc] initWithFrame:CGRectMake(interx, ScreenHeight - 63 - 15 - width - 60, width, width)];
//        _view2 = [[UIView alloc] initWithFrame:CGRectMake(interx + width + 30, ScreenHeight - 63 - 15 - width - 60, width, width)];
//        _view3 = [[UIView alloc] initWithFrame:CGRectMake(interx + (width + 30) * 2, ScreenHeight - 63 - 15 - width - 60, width, width)];
        _view1 = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth / 4 - width / 2, ScreenHeight - 63 - 15 - width - 60, width, width)];
        _view2 = [[UIView alloc] initWithFrame:CGRectMake(interx + width + 30, ScreenHeight - 63 - 15 - width - 60, width, width)];
        _view3 = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth * 3 / 4 - width / 2, ScreenHeight - 63 - 15 - width - 60, width, width)];
        _view1.backgroundColor = [UIColor whiteColor];
        _view2.backgroundColor = [UIColor whiteColor];
        _view3.backgroundColor = [UIColor whiteColor];
        _view1.layer.masksToBounds = YES;
        _view1.layer.cornerRadius = width / 2;
        _view2.layer.masksToBounds = YES;
        _view2.layer.cornerRadius = width / 2;
        _view3.layer.masksToBounds = YES;
        _view3.layer.cornerRadius = width / 2;
        _view1.tag = kViewTag;
        _view2.tag = kViewTag + 1;
        _view3.tag = kViewTag + 2;
        [self addSubview:_view1];
        [self addSubview:_view2];
        [self addSubview:_view3];
        
        _tribeview = [[UIView alloc] initWithFrame:CGRectMake(interx, ScreenHeight - 63 - 15 - width - 60 - 140 - 40, ScreenWidth - 30, 140)];
        _tribeview.backgroundColor = [UIColor whiteColor];
        [self addSubview:_tribeview];
        _tribeview.layer.masksToBounds = YES;
        _tribeview.layer.cornerRadius = 4.f;
        _tribeview.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapt = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTribe:)];
        tapt.numberOfTapsRequired = 1;
        [_tribeview addGestureRecognizer:tapt];
        
        _view1.userInteractionEnabled = YES;
        _view2.userInteractionEnabled = YES;
        _view3.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPub:)];
        tap2.numberOfTapsRequired = 1;
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPub:)];
        tap3.numberOfTapsRequired = 1;
        UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPub:)];
        tap4.numberOfTapsRequired = 1;
        [_view1 addGestureRecognizer:tap2];
        [_view2 addGestureRecognizer:tap3];
        [_view3 addGestureRecognizer:tap4];
        
        UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 4 - width / 2, ScreenHeight - 63 - 15 - 60, width, 30)];
        lable1.font = [FontTool fontWithSize:14];
        lable1.textAlignment = NSTextAlignmentCenter;
        lable1.textColor = [UIColor whiteColor];
        lable1.text = @"发文章";
        UILabel *lable2 = [[UILabel alloc] initWithFrame:CGRectMake(interx + width + 30, ScreenHeight - 63 - 15 - 60, width, 30)];
        lable2.font = [FontTool fontWithSize:14];
        lable2.textAlignment = NSTextAlignmentCenter;
        lable2.textColor = [UIColor whiteColor];
        lable2.text = @"发作品";
        lable2.hidden = YES;
        UILabel *lable3 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth * 3/ 4 - width / 2, ScreenHeight - 63 - 15 - 60, width, 30)];
        lable3.font = [FontTool fontWithSize:14];
        lable3.textAlignment = NSTextAlignmentCenter;
        lable3.textColor = [UIColor whiteColor];
        lable3.text = @"发帖子";
        [self addSubview:lable1];
        [self addSubview:lable2];
        [self addSubview:lable3];
        
        CGFloat imgwidth = width - 60;
        CGFloat startx = 30;
        if ([[DeviceHelper getPhoneType] isEqualToString:@"iPhone5_5S"]) {
            imgwidth = width - 30;
            startx = 15;
        }
        UIImageView *img1 = [[UIImageView alloc] initWithFrame:CGRectMake(startx, startx, imgwidth, imgwidth)];
        UIImageView *img2 = [[UIImageView alloc] initWithFrame:CGRectMake(startx, startx, imgwidth, imgwidth)];
        UIImageView *img3 = [[UIImageView alloc] initWithFrame:CGRectMake(startx, startx, imgwidth, imgwidth)];
        img1.image = [UIImage imageNamed:@"pubarticle"];
        img2.image = [UIImage imageNamed:@"pubgood"];
        img3.image = [UIImage imageNamed:@"pubtopic"];
        img1.contentMode = UIViewContentModeScaleAspectFill;
        img2.contentMode = UIViewContentModeScaleAspectFill;
        img3.contentMode = UIViewContentModeScaleAspectFill;
        [_view1 addSubview:img1];
        [_view2 addSubview:img2];
        [_view3 addSubview:img3];
        
        UIImageView *write = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 30, 30)];
        write.image = [UIImage imageNamed:@"pubwrite"];
        [_tribeview addSubview:write];
        UILabel *labelcreate = [[UILabel alloc] initWithFrame:CGRectMake(20 + 30 + 10, 0, 100, 70)];
        labelcreate.font = [FontTool fontWithSize:18];
        labelcreate.textAlignment = NSTextAlignmentCenter;
        labelcreate.textColor = [UIColor blackColor];
        labelcreate.text = @"创建部落";
        [_tribeview addSubview:labelcreate];
        UIImageView *goright = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 30 - 16 - 10, 27, 16, 16)];
        goright.image = [UIImage imageNamed:@"pubgo"];
        [_tribeview addSubview:goright];
        UILabel *labelinfo = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, ScreenWidth - 30 - 40, 70)];
        labelinfo.font = [FontTool fontWithSize:14];
        if ([[DeviceHelper getPhoneType] isEqualToString:@"iPhone5_5S"]) {
            labelinfo.font = [FontTool fontWithSize:13];
        }
        labelinfo.textAlignment = NSTextAlignmentLeft;
        labelinfo.textColor = [UIColor colorWithHexString:@"9b9b9b"];
        labelinfo.numberOfLines = 3;
        labelinfo.text = @"我们只能通过观赏去欣赏艺术，每个人对于艺术品的感觉也各不相同，如果用一句话定义一件艺术品，你会如何定义呢。";
        [_tribeview addSubview:labelinfo];
        _view2.hidden = YES;
    }
    
    return self;
}

- (void)clickBg:(UITapGestureRecognizer*)recognizer {
    [self removeFromSuperview];
}

- (void)clickPub:(UITapGestureRecognizer*)recognizer {
    DYLog(@"tag:%ld", recognizer.view.tag);
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    CYLTabBarController *mvc = (CYLTabBarController *)appDelegate.window.rootViewController;
    UINavigationController *viewController = (UINavigationController *)mvc.selectedViewController;
    
    if (recognizer.view.tag == kViewTag) {
        PublishArticleViewController *vc = [[PublishArticleViewController alloc] initWithNibName:@"PublishArticleViewController" bundle:nil];
        vc.mytype = PublishArticleNormal;
        [viewController pushViewController:vc animated:YES];
    } else if (recognizer.view.tag == kViewTag + 1){
        [self checkSeller];
        PublishGoodViewController *vc = [[PublishGoodViewController alloc] initWithNibName:@"PublishGoodViewController" bundle:nil];
        [viewController pushViewController:vc animated:YES];
    } else {
        PublishTopicViewController *vc = [[PublishTopicViewController alloc] initWithNibName:@"PublishTopicViewController" bundle:nil];
        [viewController pushViewController:vc animated:YES];
    }
    
    [self removeFromSuperview];
}

- (void)checkSeller {
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/userCenter/isSeller", APIYIGOU];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSInteger result = [[dict objectForKey:@"data"] integerValue];
        if (result == 0) {
           DYLog(@"not ok");
        } else {
           DYLog(@"is ok");
        }
    } error:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:@"请求失败！"];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

- (void)clickTribe:(UITapGestureRecognizer*)recognizer {
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    CYLTabBarController *mvc = (CYLTabBarController *)appDelegate.window.rootViewController;
    UINavigationController *viewController = (UINavigationController *)mvc.selectedViewController;
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TribeStoryBoard" bundle:[NSBundle mainBundle]];
    CreateTribeViewController *createVC = [storyboard instantiateViewControllerWithIdentifier:@"CreateTribeViewController"];
    //tribeID = createVC.tribeID;
    createVC.tribeID = @"0";
    [viewController pushViewController:createVC animated:YES];
    [self removeFromSuperview];
}

- (void)clickCancel:(id)sender {
    [self removeFromSuperview];
}


@end
